package com.study.example.gameserver.module.backpack.use.handler.impl;

import com.sparrow.common.handler.Handler;
import com.sparrow.common.handler.HandlerManager;
import com.study.example.gameserver.module.backpack.use.ItemUseContext;
import com.study.example.gameserver.module.backpack.use.ItemUseHandler;
import com.study.example.gameserver.module.backpack.use.handler.AbstractItemUseHandler;
import com.study.example.gameserver.module.backpack.use.handler.ItemUseWithStateHandler;
import com.study.example.gameserver.module.currency.PlayerCurrencyService;
import com.study.example.gameserver.module.currency.domain.CurrencyType;
import com.study.example.gameserver.module.currency.entity.PlayerCurrency;
import com.study.example.gameserver.module.player.domain.PlayerDataType;
import com.study.example.gameserver.module.player.entity.Player;
import com.study.example.gameserver.module.player.service.PlayerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class AddCurrencyHandler extends AbstractItemUseHandler {

    @Autowired
    PlayerService playerService;

    @Autowired
    PlayerCurrencyService currencyService;

    @Override
    public ItemUseWithStateHandler createHandler(String useParam) {
        return new AddCurrencyWithStateHandler(useParam);
    }

    @Override
    public String getHandlerKey() {
        return ItemUseHandler.AddCurrency;
    }

    /**
     * 带状态的处理器
     */
    public class AddCurrencyWithStateHandler implements ItemUseWithStateHandler {
        private int type;
        private int val;

        public AddCurrencyWithStateHandler(String useParam) {
            String[] split = useParam.split(",");
            this.type = Integer.parseInt(split[0]);
            this.val = Integer.parseInt(split[0]);
        }

        public boolean handle(ItemUseContext context) {
            Player player = context.getPlayer();
            PlayerCurrency playerCurrency = player.getData(PlayerDataType.Currency);
            CurrencyType currencyType = CurrencyType.of(this.type);
            playerCurrency.addCurrency(currencyType, context.getNum() * this.val);
            currencyService.sendCurrencyInfo(player);
            return true;
        }
    }



}
